<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Models\Luo;
use App\Models\Post;
use App\Guards\UserGuard;

class LuoController extends Controller {

    public function __construct() {
        $this->middleware('patch_wechat');
        $this->middleware('need_user_login')->except(['index', 'page']);
    }

    public function index() {
        $luo_list = Luo::all();
        $is_login = (bool)session('app_uid');
        return view('app.luo.index', ['luo_list' => $luo_list, 'is_login' => $is_login]);
    }

    public function my_luo() {
        $user = request()->app_user;
        $luo = $user->get_luo();
        if($luo){
            return redirect('/luo/' . $luo->id);
        }else{
            return redirect('/')->with('error', '您还没有所属部落!');
        }
    }

    public function luo($id) {
        $luo = Luo::findOrFail($id);
        if(UserGuard::user_can_visit_luo(request()->app_user, $luo)){
            $posts = $luo->get_posts();
            return view('app.luo.luo', ['luo' => $luo, 'posts' => $posts]);
        }else{
            return redirect('/')->with('error', '您无权访问此部落');
        }
    }

    public function post($id) {
        $post = Post::findOrFail($id);
        if(UserGuard::user_can_visit_post(request()->app_user, $post)){
            return view('app.post.post', ['post' => $post]);
        }else{
            return redirect('/')->with('error', '您无权访问此诗文');
        }
    }

    public function add_post($id) {
        $luo = Luo::findOrFail($id);
        if(request()->isMethod('post') && UserGuard::user_can_add_post(request()->app_user, $luo)){
            $post = new Post;
            $post->title = request()->input('title');
            $post->content = request()->input('content');
            $post->uid = request()->app_user->id;
            $post->luo_id = request()->app_user->get_luo()->id;
            $post->save();
            return view('app.post.add', ['success'=>1]);
        }
        return view('app.post.add');
    }

    public function get_comments($id) {
        $post = Post::findOrFail($id);
        if(UserGuard::user_can_visit_post(request()->app_user, $post) && $post->is_public()){
            $comments = $post->comments()->paginate(20);
            $r = [];
            foreach($comments as $c){
                $r[] = [
                    'id'    =>  $c->id,
                    'content'   =>  $c->content,
                    'created_at' => $c->created_at->format('Y/m/d H:i'),
                    'uid'   =>  $c->uid,
                    'username'  =>  $c->user->name
                ];
            }
            return response()->json([
                'total' =>  $comments->total(),
                'last_page' =>  $comments->lastPage(),
                'p'     =>  $comments->currentPage(),
                'data'  =>  $r
            ]);
        }
    }

    public function comment($id) {
        $post = Post::findOrFail($id);
        if(UserGuard::user_can_visit_post(request()->app_user, $post) && $post->is_public()){
            $comment = $post->add_comment(request()->input('content'), request()->app_user);
            $comment->username = $comment->user->name;
            $comment->time = $comment->created_at->format('Y/m/d H:i');
            return response()->json([
                'r' =>  1, 'comment'    =>  $comment
            ]);
        }
    }

    public function page($page) {
        if(!in_array($page, ['users', 'info', 'rule'])) abort(404);
        return view('app.luo.pages.' . $page);
    }

    public function event($id) {
        $luo = Luo::findOrFail($id);
        if(UserGuard::user_can_visit_luo(request()->app_user, $luo)){
            return view('app.luo.event', ['luo' => $luo]);
        }else{
            return redirect('/')->with('error', '您无权访问此部落');
        }
    }

    public function users($id) {
        $luo = Luo::findOrFail($id);
        if(UserGuard::user_can_visit_luo(request()->app_user, $luo)){
            $relations = $luo->user_relation_list()->paginate(20);
            return view('app.luo.users', ['luo' => $luo, 'relations' => $relations]);
        }else{
            return redirect('/')->with('error', '您无权访问此部落');
        }
    }
}
